package com.bytedance.retrofit2;

import com.bytedance.retrofit2.client.Header;
import com.bytedance.retrofit2.mime.MimeUtil;
import com.bytedance.retrofit2.mime.TypedByteArray;
import com.bytedance.retrofit2.mime.TypedInput;
import com.bytedance.retrofit2.http.Streaming;
import com.bytedance.retrofit2.mime.TypedOutput;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

final class BuiltInConverters extends Converter.Factory {
    BuiltInConverters() {

    }

    public final Converter a(Type arg3) {
        Converter v0_1;
        if (TypedOutput.class.isAssignableFrom(Utils.getRawType(arg3))) {
            v0_1 = a$d.a;
        } else {
            v0_1 = null;
        }

        return v0_1;
    }

    public final Converter responseBodyConverter(Type type, Annotation[] annotations) {
        Converter v0_1;
        if (type == TypedInput.class) {
            if (Utils.a(annotations, Streaming.class)) {
                v0_1 = a$e.a;
            } else {
                v0_1 = a$a.a;
            }
        } else if (type == String.class) {
            v0_1 = g.a;
        } else if (type == Void.class) {
            v0_1 = i.a;
        } else {
            v0_1 = null;
        }

        return v0_1;
    }

    public final Converter findToStringConverter(Type arg2) {
        Converter v0_1;
        if (arg2 == Object.class) {
            v0_1 = c.a;
        } else {
            v0_1 = null;
        }

        return v0_1;
    }

    public final Converter c(Type arg2) {
        Converter v0_1;
        if (arg2 == String.class) {
            v0_1 = a$f.a;
        } else {
            v0_1 = null;
        }

        return v0_1;
    }

    public final Converter d(Type arg2) {
        b v0;
        if (arg2 == Header.class) {
            v0 = b.a;
        } else {
            v0 = null;
        }

        return v0;
    }

    static final class a$a implements Converter {
        static final a$a a;

        static {
            a = new a$a();
        }

        a$a() {

        }

        private static TypedInput a(TypedInput arg3) throws IOException {
            TypedByteArray v3 = null;
            if (arg3 != null && !(arg3 instanceof TypedByteArray)) {
                String v0 = arg3.mimeType();
                InputStream v1 = arg3.in();
                try {
                    v3 = new TypedByteArray(v0, Utils.a(v1));
                    if (v1 == null) {
                        return ((TypedInput) v3);
                    }
                } catch (Throwable v0_1) {
                    if (v1 != null) {
                        try {
                            v1.close();
                        } catch (IOException v1_1) {
                        }
                    }

                    throw v0_1;
                }

                try {
                    v1.close();
                } catch (IOException v0_2) {
                }
            }

            return ((TypedInput) v3);
        }

        public final Object convert(Object value) throws IOException {
            return a$a.a(((TypedInput) value));
        }
    }

    static final class i implements Converter {
        static final i a;

        static {
            a = new i();
        }

        i() {

        }

        public final Object convert(Object value) throws IOException {
            InputStream v0 = ((TypedInput) value).in();
            if (v0 != null) {
                v0.close();
            }

            return null;
        }
    }

    static final class b implements Converter {
        static final b a;

        static {
            a = new b();
        }

        b() {

        }

        public final Object convert(Object value) throws IOException {
            return value;
        }
    }

    static final class c implements Converter {
        static final c a;

        static {
            a = new c();
        }

        c() {

        }

        public final Object convert(Object value) throws IOException {
            return value;
        }
    }

    static final class a$d implements Converter {
        static final a$d a;

        static {
            a = new a$d();
        }

        a$d() {

        }

        public final Object convert(Object value) throws IOException {
            return value;
        }
    }

    static final class a$e implements Converter {
        static final a$e a;

        static {
            a = new a$e();
        }

        a$e() {

        }

        public final Object convert(Object value) throws IOException {
            return value;
        }
    }

    static final class a$f implements Converter {
        static final a$f a;

        static {
            a = new a$f();
        }

        a$f() {

        }

        public final Object convert(Object value) throws IOException {
            return value;
        }
    }

    static final class g implements Converter {
        static final g a;

        static {
            a = new g();
        }

        g() {

        }

        public final Object convert(Object value) throws IOException {
            Object v0_1;
            if ((value instanceof TypedByteArray)) {
                String v0 = "UTF-8";
                if (((TypedInput) value).mimeType() != null) {
                    v0 = MimeUtil.parseCharset(((TypedInput) value).mimeType(), v0);
                }

                v0_1 = new String(((TypedByteArray) value).bytes, v0);
            } else {
                v0_1 = null;
            }

            return v0_1;
        }
    }

    static final class ToStringConverter implements Converter {
        static final ToStringConverter INSTANCE;

        static {
            INSTANCE = new ToStringConverter();
        }

        ToStringConverter() {

        }

        public final Object convert(Object value) throws IOException {
            return String.valueOf(value);
        }
    }
}

